package com.walker.kuyun.ui.activity

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.shuyu.gsyvideoplayer.GSYBaseActivityDetail
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.model.VideoOptionModel
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer
import com.walker.kuyun.App
import com.walker.kuyun.R
import com.walker.kuyun.bean.FavoriteTMBean
import com.walker.kuyun.bean.TMEpisodeDataBean
import com.walker.kuyun.config.Api
import com.walker.kuyun.config.Const
import com.walker.kuyun.database.getAppDataBase
import com.walker.kuyun.databinding.ActivityPlayBinding
import com.walker.kuyun.ui.adapter.EpisodeItemDecoration
import com.walker.kuyun.ui.adapter.PlayAdapter
import com.walker.kuyun.ui.adapter.TMDetailAdapter
import com.walker.kuyun.ui.component.MyVideoPlayer
import com.walker.kuyun.ui.fragment.ShareDialogFragment
import com.walker.kuyun.util.Episode2ViewHolder
import com.walker.kuyun.util.Util.dp2px
import com.walker.kuyun.util.Util.getResColor
import com.walker.kuyun.util.Util.setColorStatusBar
import com.walker.kuyun.util.Util.showToast
import com.walker.kuyun.util.downloadanime.AnimeDownloadHelper
import com.walker.kuyun.util.gone
import com.walker.kuyun.viewmodel.PlayViewModel
import kotlinx.coroutines.*
import tv.danmaku.ijk.media.player.IjkMediaPlayer


class PlayActivity : GSYBaseActivityDetail<MyVideoPlayer>(),
    CoroutineScope by CoroutineScope(Dispatchers.IO + SupervisorJob()) {
    private lateinit var mBinding: ActivityPlayBinding
    private var isFavorite: Boolean = false
    private var favoriteBeanDataReady: Int = 0
        set(value) {
            field = value
            if (value == 2) mBinding.ivPlayActivityFavorite.isEnabled = true
        }
    private lateinit var videoPlayer: MyVideoPlayer
    private var partUrl: String = ""
    private var detailPartUrl: String = ""
    private lateinit var viewModel: PlayViewModel
    private lateinit var adapter: PlayAdapter
    private var isFirstTime = true
    private var isParsed = false
    override fun onDestroy() {
        super.onDestroy()
        cancel()
        mBinding.playActivityGetUrl.apply {
            clearHistory()
            clearCache(true);
            loadUrl("about:blank");
            freeMemory();
            pauseTimers();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityPlayBinding.inflate(layoutInflater)
        setContentView(mBinding.root)

        setColorStatusBar(window, Color.BLACK)

        viewModel = ViewModelProvider(this).get(PlayViewModel::class.java)
        adapter = PlayAdapter(this, viewModel.playBeanDataList)

        videoPlayer = findViewById(R.id.avp_play_activity)
        initVideoBuilderMode()

        videoPlayer.getDownloadButton()?.setOnClickListener {
            getSheetDialog("download").show()
        }

        //设置返回按键功能
        videoPlayer.backButton?.setOnClickListener { onBackPressed() }

        partUrl = intent.getStringExtra("partUrl") ?: ""
        detailPartUrl = intent.getStringExtra("detailPartUrl") ?: ""

        //分享按钮
        videoPlayer.getShareButton()?.setOnClickListener {
            ShareDialogFragment().setShareContent(Api.MAIN_URL + viewModel.partUrl)
                .show(supportFragmentManager, "share_dialog")
        }

        mBinding.run {
            rvPlayActivity.layoutManager = LinearLayoutManager(this@PlayActivity)
            rvPlayActivity.setHasFixedSize(true)
            rvPlayActivity.adapter = adapter

            srlPlayActivity.setOnRefreshListener { viewModel.getPlayData(partUrl) }
            srlPlayActivity.setColorSchemeResources(R.color.main_color)

            playActivityGetUrl.settings.apply {
                javaScriptEnabled = true
            }
            playActivityGetUrl.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    view!!.loadUrl(url!!)
                    return true
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    playActivityGetUrl.evaluateJavascript("originalText") {//,redirecturl+mp4]
                        Log.d(TAG, "onCreate: $it")
                    }
                    playActivityGetUrl.evaluateJavascript("originalText") {//,redirecturl+mp4]
                        Log.d(TAG, "onCreate: $it")
                    }
                    playActivityGetUrl.evaluateJavascript("let res = eval(originalText);redirecturl+playlist[0][\"url\"]") {//,redirecturl+mp4]
                        Log.d(TAG, "onCreate: $it")
                        isParsed = true
                        startPlay2(it.substring(1, it.length - 1), "")
                    }
                }
            }
        }

        launch {
            val favoriteAnime = getAppDataBase().favoriteAnimeDao().getFavoriteAnime(detailPartUrl)
            withContext(Dispatchers.Main) {
                isFavorite = if (favoriteAnime == null) {
                    mBinding.ivPlayActivityFavorite.setImageResource(R.drawable.ic_star_border_main_color_2_24)
                    false
                } else {
                    mBinding.ivPlayActivityFavorite.setImageResource(R.drawable.ic_star_main_color_2_24)
                    true
                }
                mBinding.ivPlayActivityFavorite.setOnClickListener {
                    if (isFavorite) {
                        getAppDataBase().favoriteAnimeDao().deleteFavoriteAnime(detailPartUrl)

                        isFavorite = false
                        mBinding.ivPlayActivityFavorite.setImageResource(R.drawable.ic_star_border_main_color_2_24)
                        getString(R.string.remove_favorite_succeed).showToast()

                    } else {
                        getAppDataBase().favoriteAnimeDao().insertFavoriteAnime(
                            FavoriteTMBean(
                                Const.ViewHolderTypeString.ITEM_COVER_8, "",
                                detailPartUrl,
                                viewModel.playBean?.title?.title ?: "",
                                System.currentTimeMillis(),
                                viewModel.animeCover,
                                lastEpisodeUrl = viewModel.partUrl,
                                lastEpisode = viewModel.animeEpisodeDataBean.title
                            )
                        )
                        isFavorite = true
                        mBinding.ivPlayActivityFavorite.setImageResource(R.drawable.ic_star_main_color_2_24)
                        getString(R.string.favorite_succeed).showToast()
                    }
                }
            }
        }
        mBinding.ivPlayActivityFavorite.isEnabled = false

        viewModel.mldAnimeCover.observe(this, {
            if (it) {
                favoriteBeanDataReady++
            }
        })

        viewModel.mldPlayBean.observe(this, {
            mBinding.srlPlayActivity.isRefreshing = false

            val title = viewModel.playBean?.title?.title
            mBinding.tvPlayActivityTitle.text = title

            adapter.notifyDataSetChanged()

            favoriteBeanDataReady++

            if (isFirstTime) {
                videoPlayer.startPlay()
                isFirstTime = false
            }
        })

        //缓存番剧调用getAnimeEpisodeData()来获取视频url
        viewModel.mldGetAnimeEpisodeData.observe(this, {
            val url = viewModel.episodesList[it].videoUrl
            AnimeDownloadHelper.instance.downloadAnime(
                this,
                url,
                viewModel.playBean?.title?.title + "/" +
                        viewModel.episodesList[it].title
            )
        })

        mBinding.srlPlayActivity.isRefreshing = true
        viewModel.getPlayData(partUrl)
//        viewModel.getAnimeCover(if (TextUtils.isEmpty(detailPartUrl)) partUrl else detailPartUrl)

        val videoOptionModel =
            VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1)
        GSYVideoManager.instance().optionModelList = listOf(videoOptionModel)
    }

    fun startPlay(url: String, currentEpisodeIndex: Int, title: String) {
        viewModel.mldAnimeEpisodeDataRefreshed.observe(this, {
            if (it) {
                videoPlayer.currentPlayer.startPlay()
            }
        })
        viewModel.refreshAnimeEpisodeData(url, currentEpisodeIndex, title)
    }

    fun startPlay2(url: String, title: String) {
        videoPlayer.startPlay(url, title)
    }

    private fun GSYBaseVideoPlayer.startPlay(url: String = "", title: String = "") {
        GSYVideoType.disableMediaCodec()        // 关闭硬解码
        //设置播放URL
        if (url.isBlank()) {
            if (!isDestroyed) {
                viewModel.updateFavoriteData(
                    detailPartUrl,
                    viewModel.partUrl,
                    viewModel.animeEpisodeDataBean.title,
                    System.currentTimeMillis()
                )
                viewModel.insertHistoryData(detailPartUrl)
            }
            setUp(
                viewModel.animeEpisodeDataBean.videoUrl.apply {
                    Log.d("PlayTest", "${viewModel.animeEpisodeDataBean.title} startPlay: $this")
                },
                false, viewModel.animeEpisodeDataBean.title
            )
        } else {
            if (!isDestroyed) {
                viewModel.updateFavoriteData(
                    detailPartUrl, viewModel.partUrl, title,
                    System.currentTimeMillis()
                )
                viewModel.insertHistoryData(detailPartUrl)
            }
            setUp(url, false, title)
        }
        //开始播放
        startPlayLogic()
    }

    override fun onPlayError(url: String?, vararg objects: Any?) {
        super.onPlayError(url, *objects)
        if (!isParsed)
            mBinding.playActivityGetUrl.loadUrl(url!!)
        else
            (objects[0].toString() + ", " + getString(R.string.get_data_failed)).showToast()
    }

    override fun onPrepared(url: String?, vararg objects: Any?) {
        super.onPrepared(url, *objects)
        //调整触摸滑动快进的比例
        //毫秒,刚好划一屏1分35秒
        videoPlayer.seekRatio = videoPlayer.duration / 90_000f
    }

    override fun getGSYVideoPlayer(): MyVideoPlayer = videoPlayer

    override fun getGSYVideoOptionBuilder(): GSYVideoOptionBuilder {
        return GSYVideoOptionBuilder()
            .setReleaseWhenLossAudio(false)         //音频焦点冲突时是否释放
            .setPlayTag(this.javaClass.simpleName)  //防止错位设置
            .setIsTouchWiget(true)
            .setCachePath(getExternalFilesDir(null))
            .setCacheWithPlay(true)
            .setRotateViewAuto(false)
            .setLockLand(false)
            .setShowFullAnimation(false)            //打开动画
            .setNeedLockFull(true)
            .setDismissControlTime(5000)
    }

    override fun clickForFullScreen() {}

    override fun getDetailOrientationRotateAuto(): Boolean = true

    fun getSheetDialog(action: String): BottomSheetDialog {
        val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialogTheme)
        val contentView = View.inflate(this, R.layout.dialog_bottom_sheet_2, null)
        bottomSheetDialog.setContentView(contentView)
        val tvTitle = contentView.findViewById<TextView>(R.id.tv_dialog_bottom_sheet_2_title)
        tvTitle.text = when (action) {
            "play" -> getString(R.string.play_list)
            "download" -> getString(R.string.download_anime)
            else -> ""
        }
        val recyclerView = contentView.findViewById<RecyclerView>(R.id.rv_dialog_bottom_sheet_2)
        recyclerView.layoutManager = GridLayoutManager(this, 3)
        recyclerView.post {
            recyclerView.setPadding(
                dp2px(16f), dp2px(16f),
                dp2px(16f), dp2px(16f)
            )
            recyclerView.scrollToPosition(0)
        }
        if (recyclerView.itemDecorationCount == 0) {
            recyclerView.addItemDecoration(EpisodeItemDecoration())
        }
        val adapter = EpisodeListAdapter(
            this,
            viewModel.episodesList,
            bottomSheetDialog,
            1,
            action
        )
        recyclerView.adapter = adapter
        viewModel.mldEpisodesList.observe(this, {
            adapter.notifyDataSetChanged()
            mBinding.avpPlayActivity.setEpisodeAdapter(
                PlayerEpisodeRecyclerViewAdapter(
                    this,
                    viewModel.episodesList
                )
            )
        })
        return bottomSheetDialog
    }

    class EpisodeListAdapter(
        private val activity: PlayActivity,
        private val dataList: List<TMEpisodeDataBean>,
        private val dialog: Dialog? = null,
        private val showType: Int = 0,    //0是横向，1是三列
        private val action: String = "play"
    ) : TMDetailAdapter.EpisodeList1Adapter(activity, dataList, dialog, showType) {

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val item = dataList[position]

            when (holder) {
                is Episode2ViewHolder -> {
                    holder.tvAnimeEpisode2.text = item.title
                    if (item.alreadyPlay) {
                        holder.itemView.setBackgroundResource(R.color.foreground_main_color_2)
                        holder.tvAnimeEpisode2.setTextColor(
                            activity.getResColor(R.color.white)
                        )
                    } else {
                        holder.tvAnimeEpisode2.setTextColor(
                            activity.getResColor(R.color.foreground_main_color_2)
                        )
                        holder.itemView.setBackgroundResource(R.color.white)
                    }
                    val layoutParams = holder.itemView.layoutParams
                    if (showType == 0) {
                        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
                        if (layoutParams is ViewGroup.MarginLayoutParams) {
                            layoutParams.setMargins(0, dp2px(5f), dp2px(10f), dp2px(5f))
                        }
                        holder.itemView.layoutParams = layoutParams
                    } else {
                        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
                        holder.itemView.setPadding(0, dp2px(10f), 0, dp2px(10f))
                        holder.itemView.layoutParams = layoutParams
                    }
                    if (action == "play") {
                        holder.itemView.setOnClickListener {
                            activity.startPlay(item.actionUrl, position, item.title)
                            dialog?.dismiss()
                        }
                    } else if (action == "download") {
                        holder.itemView.setOnClickListener {
                            activity.getString(R.string.parsing_video).showToast()
                            activity.viewModel.getAnimeEpisodeData(item.actionUrl, position)
                        }
                    }
                }
                else -> {
                    holder.itemView.visibility = View.GONE
                    (App.getApp().resources.getString(R.string.unknown_view_holder) + position).showToast()
                }
            }
        }
    }

    class PlayerEpisodeRecyclerViewAdapter(
        private val activity: PlayActivity,
        private val dataList: List<TMEpisodeDataBean>
    ) : MyVideoPlayer.EpisodeRecyclerViewAdapter(activity, dataList) {

        override val currentIndex: Int
            get() = activity.viewModel.currentEpisodeIndex

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            val item = dataList[position]

            when (holder) {
                is MyVideoPlayer.RightRecyclerViewViewHolder -> {
                    holder.tvTitle.setTextColor(
                        activity.getResColor(
                            if (item.title == activity.viewModel.animeEpisodeDataBean.title)
                                R.color.unchanged_main_color_2
                            else R.color.foreground_white
                        )
                    )
                    holder.tvTitle.text = item.title
                    holder.itemView.setOnClickListener {
                        activity.mBinding.avpPlayActivity.currentPlayer.run {
                            if (this is MyVideoPlayer) {
                                getRightContainer()?.gone()
                                // 因为右侧界面显示时，不在xx秒后隐藏界面，所以要恢复xx秒后隐藏控制界面
                                enableDismissControlViewTimer(true)
                            }
                        }
                        activity.startPlay(item.actionUrl, position, item.title)
                    }
                }
                else -> {
                    holder.itemView.visibility = View.GONE
                    (App.getApp().resources.getString(R.string.unknown_view_holder) + position).showToast()
                }
            }
        }
    }

    companion object {
        const val TAG = "PlayActivity"
    }
}